package cn.edu.lingnan.fruitshop.service.impl;

import cn.edu.lingnan.fruitshop.common.JSONResult;
import cn.edu.lingnan.fruitshop.mapper.OrdertableMapper;
import cn.edu.lingnan.fruitshop.pojo.Orderdetail;
import cn.edu.lingnan.fruitshop.mapper.OrderdetailMapper;
import cn.edu.lingnan.fruitshop.pojo.Ordertable;
import cn.edu.lingnan.fruitshop.pojo.User;
import cn.edu.lingnan.fruitshop.service.OrderdetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张小凡
 * @since 2022-01-04
 */
@Service
public class OrderdetailServiceImpl extends ServiceImpl<OrderdetailMapper, Orderdetail> implements OrderdetailService {
    @Autowired(required = false)
    private OrdertableMapper ordertableMapper;
    @Autowired(required = false)
    private OrderdetailMapper orderdetailMapper;
    @Override
    public JSONResult<Integer> submitOrder(String goodIds, String goodNums, String allPrice, HttpSession session) {
        User user = (User)session.getAttribute("user");
        if(user == null)
        {
            return new JSONResult<>(201,"用户未登录");
        }
        Ordertable ordertable = new Ordertable().setUserId(user.getUserId()).setOrderDate(new Date()).setStatus(1).setPrice(Double.valueOf(allPrice));
        int insert = ordertableMapper.insert(ordertable);
        String[] idArr = goodIds.split(",");
        String[] numArr = goodNums.split(",");
        Orderdetail orderdetail = null;
        for (int i = 0; i < idArr.length; i++) {
            orderdetail = new Orderdetail().setOrdedetailId(ordertable.getId())
            .setGoodId(Integer.parseInt(idArr[i])).setNum(Integer.parseInt(numArr[i]));
            orderdetailMapper.insert(orderdetail);
        }
        return new JSONResult<>(200,"订单成功生成",ordertable.getId());
    }
}
